package com.example.landlord.demo.utils.system;

import java.util.ArrayList;
import java.util.Random;

class PuckerStep{
    //扑克牌步奏类，记录每一次玩家出的牌的牌型和牌值
    DefinePucker definePucker;
    int puckerValue;
    int length;
    Player player;
}

public class PuckerSystem {
    Puker[] pukerList;
    public PuckerSystem() {
        init();
    }

    //初始化
    public void init() {
        pukerList = new Puker[54];
        ArrayList<Puker> pukerArrayList = new ArrayList<Puker>();
        for (short i = 0; i < 4; i++) {
            for (short j = 1; j < 14; j++) {
                Puker p = new Puker();
                p.color = i;
                p.pName = j + "";
                p.value = j;
                if (j == 1) {
                    p.pName = "A";
                    p.value=14;
                } else if (j == 2) {
                    p.value = 15;
                } else if (j == 11) {
                    p.pName = "J";
                } else if (j == 12) {
                    p.pName = "Q";
                } else if (j == 13) {
                    p.pName = "K";
                }
                pukerArrayList.add(p);
            }
        }
        Puker bigKing = new Puker();
        bigKing.pName = "大王";
        bigKing.value = 17;
        bigKing.color = -1;
        pukerArrayList.add(bigKing);
        Puker lowKing = new Puker();
        lowKing.pName = "小王";
        lowKing.value = 16;
        lowKing.color = -1;
        pukerArrayList.add(lowKing);

        this.pukerList = pukerArrayList.toArray(pukerList);
        showAllPucker();
    }

    //显示所有牌
    public void showAllPucker() {
        for (Puker p : pukerList) {
            System.out.println(p.toString());
        }
    }

    //洗牌
    public void shuffle() {
        Random random = new Random();
        for (int i = 0; i < pukerList.length; i++) {
            int r = random.nextInt(54);
            Puker m = pukerList[i];
            pukerList[i] = pukerList[r];
            pukerList[r] = m;
        }
        licensing(true);
        //分配id
        totalSetId();
    }

    //某玩家出牌
    public void sendPucker(int[] id, Player p) {
        String sendingPuckerNames = new String();
        for (int j = 0; j < id.length; j++) {
            Puker pucker = pukerList[id[j]];
            pucker.setStatus(PuckerStatus.SENDED);

            sendingPuckerNames += " " + pucker.getColorName() + pucker.pName;
        }
        System.out.println(p.user.name + "出了牌:" + sendingPuckerNames);
    }

    //某玩家未出牌数量
    public int countNotSend(Player p) {
        int count = 0;
        for (int i = p.index * 17; i < p.index * 17 + 17; i++) {
            if (pukerList[i].getStatus() == PuckerStatus.NOTSEND) {
                count++;
            }
        }
        return count;
    }

    //手牌排序
    public void licensing(boolean order) {
        //每个玩家手里的牌进行排序
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 16; j++) {
                for (int k = j + 1; k < 17; k++) {
                    int previous = i * 17 + j, behind = i * 17 + k;
                    //order为排序顺序，true为大到小，false为从小到大
                    if (order ? this.pukerList[previous].value < this.pukerList[behind].value : this.pukerList[previous].value > this.pukerList[behind].value) {
                        Puker m = pukerList[previous];
                        pukerList[previous] = pukerList[behind];
                        pukerList[behind] = m;
                    }
                }
            }
        }

    }

    //所有牌根据位置重新设置id
    public void totalSetId() {
        for (int j = 0; j < 54; j++) {
            pukerList[j].id = j;
        }
    }

    //判断牌型和牌值
    public PuckerStep verification(int[] sendingPuckers) {
        PuckerStep puckerStep=new PuckerStep();
        int[] temp = sendingPuckers;
        puckerStep.definePucker = DefinePucker.ILLEGAL;
        puckerStep.length = sendingPuckers.length;
        //定义一个数组，存储排序的手牌
        for (int i = 0; i < sendingPuckers.length - 1; i++) {
            for (int j = i + 1; j < sendingPuckers.length; j++) {
                int previous = pukerList[temp[i]].value;
                int behind = pukerList[temp[j]].value;
                if (previous > behind) {
                    int t = temp[i];
                    temp[i] = temp[j];
                    temp[j] = t;
                }
            }
        }
        debugShowAllP(temp);
        if (temp.length <= 4) {
            if (pukerList[temp[0]].value == pukerList[temp[temp.length - 1]].value) {
                puckerStep.puckerValue = pukerList[temp[0]].value;
                switch (temp.length) {
                    case 1:
                            puckerStep.definePucker = DefinePucker.SINGLE;
                            System.out.println("ntm出了一张单牌");
                        break;
                    case 2:
                            puckerStep.definePucker = DefinePucker.DOUBLE;

                            System.out.println("ntm出了一张对子");
                        break;
                    case 3:
                        puckerStep.definePucker = DefinePucker.THREE;
                        System.out.println("ntm出了一张三不带");
                        break;
                    case 4:
                        puckerStep.definePucker = DefinePucker.BOMB;
                        System.out.println("ntm出了一张炸弹");
                        break;
                }
            } else if (temp.length == 2 && (pukerList[temp[0]].color == -1 && pukerList[temp[1]].color == -1)) {
                puckerStep.definePucker = DefinePucker.ROCKET;
                puckerStep.puckerValue = pukerList[temp[0]].value;
                System.out.println("ntm出了一张火箭");
            } else if (isTHREEWITHONE(temp)) {
                puckerStep.definePucker = DefinePucker.THREEWITHONE;
                puckerStep=isValueEqual(temp, puckerStep);
                System.out.println("ntm出了一张三带一");
            }
        } else if (temp.length >= 5) {
            if (isCONTINUE(temp)) {
                puckerStep.definePucker = DefinePucker.CONTINUE;
                puckerStep.puckerValue = pukerList[temp[0]].value;
                System.out.println("ntm出了一张莲子");
            } else if (isContinueWithTwo(temp)) {
                puckerStep.definePucker = DefinePucker.CONTINUETWO;
                puckerStep.puckerValue = pukerList[temp[0]].value;
                System.out.println("ntm出了一张连队");
            } else if (isTHREEWITHTWO(temp)) {
                puckerStep.definePucker = DefinePucker.THREEWITHTWO;
                puckerStep=isValueEqual(temp, puckerStep);
                System.out.println("ntm出了一张三带2");
            } else if (isAir(temp)) {
                puckerStep.definePucker = DefinePucker.AIR;
                puckerStep=isValueEqual(temp, puckerStep);
                System.out.println("ntm出了一张飞机");
            } else if (isAirOne(temp)) {
                puckerStep.definePucker = DefinePucker.AIRWITHONE;
                puckerStep=isValueEqual(temp, puckerStep);
                System.out.println("ntm出了一张飞机带1");
            } else if (isAIRTWO(temp)) {
                puckerStep.definePucker = DefinePucker.AIRWITHTWO;
                puckerStep=isValueEqual(temp, puckerStep);
                    System.out.println(puckerStep.puckerValue);
                    System.out.println("one");
                System.out.println("ntm出了一张飞机带2");
            }
            //跑得快玩法中使用
//            else if(isAIRTWODIFFERENT(temp)){
//                puckerStep.definePucker=DefinePucker.THREEWITHCHAOS;
//                if(isValueEqual(temp)){}
//                System.out.println("ntm出了一张飞机带2d");
//            }
            else if (isCONTINUEBOMB(temp)) {
                puckerStep.definePucker = DefinePucker.CONTINUEBOMB;
                puckerStep.puckerValue=pukerList[temp[0]].value;
                System.out.println("ntm出了一张连炸");
            }
            else if(isFOURWITHTETWO(temp)){
                puckerStep.definePucker = DefinePucker.FOURWITHDOUBLE;
                puckerStep=isValueEqual(temp, puckerStep);
                System.out.println("ntm出了一张四代二");
            }
            else if(isFOURWITHTEFOUR(temp)){
                puckerStep.definePucker = DefinePucker.FOURWITHFOUR;
                puckerStep=isValueEqual(temp, puckerStep);
                System.out.println("ntm出了一张四代二对");
            }
        }
        switch(puckerStep.definePucker){
            case CONTINUETWO:
                if(pukerList[temp[temp.length-1]].value>14){
                    puckerStep.definePucker=DefinePucker.ILLEGAL;
                }
                break;
            case CONTINUE:
                if(pukerList[temp[temp.length-1]].value>14){
                    puckerStep.definePucker=DefinePucker.ILLEGAL;
                }
                break;
            case AIR:
                if(pukerList[temp[temp.length-1]].value>14){
                    puckerStep.definePucker=DefinePucker.ILLEGAL;
                }
                break;
            case AIRWITHONE:
                if(pukerList[temp[temp.length-1]].value>14){
                    puckerStep.definePucker=DefinePucker.ILLEGAL;
                }
                break;
            case AIRWITHTWO:
                if(pukerList[temp[temp.length-1]].value>14){
                    puckerStep.definePucker=DefinePucker.ILLEGAL;
                }
                break;
            case CONTINUEBOMB:
                if(pukerList[temp[temp.length-1]].value>14){
                    puckerStep.definePucker=DefinePucker.ILLEGAL;
                }
                break;
        }
        return puckerStep;
    }
    public boolean isTHREEWITHONE(int[] a) {
        if (a.length != 4) {
            System.out.println(3);
            return false;
        }
        for (int i = 0; i < a.length - 2; i++) {
            if (i >= a.length - 2) {
                System.out.println(2);
                return false;
            }
            if (pukerList[a[i]].value == pukerList[a[i + 1]].value && pukerList[a[i + 1]].value == pukerList[a[i + 2]].value) {
                return true;
            }
        }
        return false;
    }

    public boolean isTHREEWITHTWO(int[] a) {
        if (a.length != 5) {
            return false;
        }
        for (int i = 0; i < a.length - 2; i++) {
            if (pukerList[a[i]].value == pukerList[a[i + 1]].value && pukerList[a[i + 1]].value == pukerList[a[i + 2]].value) {
                if ((i == 0 && pukerList[a[i+3]].value == pukerList[a[i + 4]].value) ||
                        (i!=0 && pukerList[a[i - 1]].value == pukerList[a[i - 2]].value)
                        ) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    public boolean isCONTINUE(int[] a) {
        if (pukerList[a[0]].value + a.length - 1 != pukerList[a[a.length - 1]].value) {
            return false;
        }
        for (int i = 1; i < a.length - 1; i += 1) {
            if (pukerList[a[i]].value + 1 != pukerList[a[i + 1]].value) {
                return false;
            }
        }
        return true;
    }

    public boolean isContinueWithTwo(int[] a) {
        if (a.length % 2 != 0 && a.length != 2) {
            return false;
        }
        if (pukerList[a[0]].value + a.length / 2 - 1 != pukerList[a[a.length - 1]].value) {
            System.out.println(1);
            return false;
        }
        for (int i = 0; i < a.length - 2; i += 2) {
            if (pukerList[a[i]].value+1!= pukerList[a[i + 2]].value) {
                return false;
            }
        }
        return true;
    }

    public boolean isAir(int[] a) {
        if (a.length % 3 != 0 || a.length == 3) {
            return false;
        }
        if (pukerList[a[0]].value + a.length / 3 - 1 != pukerList[a[a.length - 1]].value) {
            return false;
        }
        for (int i = 0; i < a.length - 2; i += 3) {
            if (pukerList[a[i]].value != pukerList[a[i + 2]].value) {
                return false;
            }

        }
        for (int i = 0; i < a.length - 3; i += 3) {
            if (pukerList[a[i]].value + 1 != pukerList[a[i + 3]].value) {
                return false;
            }
        }
        return true;
    }

    public boolean isAirOne(int[] a) {
        if (a.length % 4 != 0 || a.length == 4) {
            return false;
        }
        int count = 0;

        int[] id = new int[5];
        for (int i = 0; i < a.length - 2; i++) {
            if (pukerList[a[i]].value == pukerList[a[i + 1]].value && pukerList[a[i + 1]].value == pukerList[a[i + 2]].value) {

                i += 2;
                id[count] = pukerList[a[i - 2]].value;
                ++count;
            }
        }
        System.out.println(count);
        for (int i = 0; i < count - 1; i++) {
            if (id[i] + 1 != id[i + 1]) {
//                System.out.println(id[i]);
                return false;
            }
        }
        if (count != a.length / 4) {
            return false;
        }

        return true;
    }

    public boolean isAIRTWO(int[] a) {
        if (a.length % 5 != 0 || a.length == 5) {
            return false;
        }
        //记录飞机个数
        int airNum=0;
        //记录对子个数
        int doubleNum=0;
        int[] id = new int[4];
        int j=0;
        for (int i = 0; i < a.length - 2; i++) {
            if (pukerList[a[i]].value == pukerList[a[i + 1]].value && pukerList[a[i + 1]].value == pukerList[a[i + 2]].value) {
                airNum++;
                id[j] = pukerList[a[i]].value;
                j++;
            }
        }
        for(int i=0;i<a.length-1;i++){
            if (pukerList[a[i]].value == pukerList[a[i + 1]].value){
                doubleNum++;
            }
        }
        System.out.println(airNum);
        System.out.println(doubleNum);
        for (int i = 0; i < airNum - 1; i++) {
            if (id[i] + 1 != id[i + 1]) {
                System.out.println(id[i]);
                return false;
            }
        }
        if(doubleNum!=3*airNum){
            return false;
        }
      return true;
    }

    //跑的快中三带2张不同的牌
    public boolean isAIRTWODIFFERENT(int[] a) {
        if (a.length % 5 != 0 && a.length != 5) {
            return false;
        }
        int count = 0;

        int[] id = new int[4];
        for (int i = 0; i < a.length - 2; i++) {
            if (pukerList[a[i]].value == pukerList[a[i + 1]].value && pukerList[a[i + 1]].value == pukerList[a[i + 2]].value) {

                i += 2;
                id[count] = pukerList[a[i - 2]].value;
                ++count;
            }
        }
        for (int i = 0; i < count - 1; i++) {
            if (id[i] + 1 != id[i + 1]) {
                System.out.println(id[i]);
                return false;
            }
        }
//        puckerStep.definePucker = DefinePucker.THREEWITHCHAOS;
        if (count != a.length / 5) {
            return false;
        }
        return true;
    }

    public boolean isFOURWITHTETWO(int[] a){
        if (a.length != 6) {
            return false;
        }
        for (int i = 0; i < a.length - 3; i++) {
            if (pukerList[a[i]].value == pukerList[a[i + 1]].value && pukerList[a[i + 1]].value == pukerList[a[i + 2]].value &&
                    pukerList[a[i]].value == pukerList[a[i + 3]].value) {
                return true;
            }
        }
       return false;
    }

    public boolean isFOURWITHTEFOUR(int[] a){
        if (a.length != 8) {
            return false;
        }
        int contineFOUR=0;
        int doubleNum=0;
        for (int i = 0; i < a.length - 3; i++) {
            if (pukerList[a[i]].value == pukerList[a[i + 1]].value && pukerList[a[i + 1]].value == pukerList[a[i + 2]].value &&
                    pukerList[a[i]].value == pukerList[a[i + 3]].value) {
                contineFOUR++;
            }
        }
        for(int i=0;i<a.length-1;i++){
            if (pukerList[a[i]].value == pukerList[a[i + 1]].value){
                doubleNum++;
            }
        }
        if(doubleNum!=5*contineFOUR){
            return false;
        }
        return true;
    }
//赋值
  public PuckerStep isValueEqual(int[] a, PuckerStep puckerStep){
      for (int i = 0; i < a.length - 2; i++) {
          if (pukerList[a[i]].value == pukerList[a[i + 1]].value && pukerList[a[i + 1]].value == pukerList[a[i + 2]].value){
              puckerStep.puckerValue = pukerList[a[i]].value;
              System.out.println("--------------");
              System.out.println(puckerStep.puckerValue);
              return puckerStep;
             }
          }
          return puckerStep;
  }
    public boolean isCONTINUEBOMB(int[] a) {
        if (a.length % 4 != 0 && a.length != 4) {
            return false;
        }
        for (int i = 0; i < a.length - 3; i += 4) {
            if (pukerList[a[i]].value != pukerList[a[i + 3]].value) {
                return false;
            }
        }
        for (int i = 0; i < a.length - 4; i += 4) {
            if (pukerList[a[i]].value + 1 != pukerList[a[i + 4]].value) {
                return false;
            }
        }
        return true;
    }
    public void debugShowAllP(int[] a) {
        System.out.println("--------排好序的手牌---------");
        for (int i = 0; i < a.length; i++) {
            System.out.println(pukerList[a[i]].toString());
        }
    }
}
